package au.com.handsunited.web.controller.group;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.Group;
import au.com.handsunited.model.User;
import au.com.handsunited.service.EntityService;
import au.com.handsunited.service.GroupService;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class JoinGroupController extends AbstractController {


	private GroupService groupService;
	private EntityService entityService;
	private String successView;

	private static Logger logger = LoggerFactory.getLogger(JoinGroupController.class);



	/**
	 *
	 */
	public JoinGroupController() {

	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String groupId = request.getParameter("id");
		Group group = groupService.getGroup(groupId);
		User currentUser = SessionSupport.getUser(request);
		groupService.addMemberToGroup(group, currentUser);
		ModelAndView mav = new ModelAndView(successView, "entity", group);
		return mav;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param groupService the groupService to set
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
